﻿using System.Threading.Tasks;

namespace WenAntdUI.Services
{
    public class IdentityService(UserService userService)
    {
        public SysUser? SysUser { get; private set; }

        /// <summary>
        /// 用户名
        /// </summary>
        public string? UserName => SysUser?.UserName;

        /// <summary>
        /// 说明
        /// </summary>
        public string? Description => SysUser?.Description;

        /// <summary>
        /// 显示名称
        /// </summary>
        public string? DisplayName => SysUser?.DisplayName;

        /// <summary>
        /// 用户密码
        /// </summary>
        public string? Password { get; private set; }
        /// <summary>
        /// 设置用户信息
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        public void SetUserInfo(string username, string password)
        {
            Password = password;
            SysUser = userService.GetUserByUserName(username);
        }

        private string? _token;
        private DateTime _tokenExpireTime = DateTime.MinValue;
        /// <summary>
        /// 获取用户Token
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetUserTokenAsync()
        {
            if (_tokenExpireTime < DateTime.Now)
            {
                _token =await new AuthClient().GetTokenAsync(UserName, Password);
                _tokenExpireTime = DateTime.Now.AddMinutes(30);
            }
            return _token;
        }
    }
}
